<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Goods extends Model
{
    protected $table = "goods";
    protected $fillable = [];
    protected $appends = ['imagelist','detaillist','headerimage'];

    public function getImagelistAttribute()
    {
        if(isset($this->attributes['image']) && !empty($this->attributes['image'])){
            $ids = explode(',',$this->attributes['image']);
            if($ids){
                return Attachments::query()->whereIn('id',$ids)->get();
            }
        }
        return [];
    }

    public function getDetaillistAttribute()
    {
        if(isset($this->attributes['detail']) && !empty($this->attributes['detail'])){
            $ids = explode(',',$this->attributes['detail']);
            if($ids){
                return Attachments::query()->whereIn('id',$ids)->get();
            }
        }
        return [];
    }

    public function getHeaderImageAttribute()
    {
        if($this->attributes['header']){
            return Attachments::query()->find($this->attributes['header']);
        }
        return null;
    }

    public function sku(){
        return $this->belongsTo(GoodsSku::class,'id','goods_id');
    }
}
